package com.tracy.lifeproject.discovery.model;

import android.os.Handler;

import com.google.gson.Gson;
import com.tracy.lifeproject.bean.HotStrategyBean;
import com.tracy.lifeproject.constant.UrlString;


import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by asus on 2016/7/20.
 */
public class StrategyHotModel {
    private onResultListener mOnResulrListener;

    public StrategyHotModel(onResultListener onResulrListener) {
        this.mOnResulrListener = onResulrListener;
    }

    public void loadDatas(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(UrlString.URL_DISCOVERY_STRATEGY).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mOnResulrListener.onFailure();
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                HotStrategyBean hotStrategyBean = new Gson().fromJson(result, HotStrategyBean.class);
                final List<HotStrategyBean.DataBean.CollectionsBean> beanList = hotStrategyBean.data.collections;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mOnResulrListener.onSuccess(beanList);
                    }
                });
            }
        });
    }

    Handler handler = new Handler();
}
